package main

import (
	"encoding/json"

	"fmt"
	"os"
	"runtime"
	"sync"

	"gitee.com/ggtony/folder-scan/filescan"
	"gitee.com/ggtony/folder-scan/getflag"
	"gitee.com/ggtony/folder-scan/task"
)

var wg sync.WaitGroup

func main() {
	var fpath string
	var outputname string
	ChoseType := new(filescan.FileTypeList)
	getflag.GetFlag(&fpath, &outputname, ChoseType)
	MaxLine := make(chan struct{}, runtime.NumCPU())
	if fpath[len(fpath)-1] == '/' {
		fpath = fpath[0 : len(fpath)-1]
	}
	//	fmt.Scanf("%s", &fpath)
	fmt.Println(ChoseType)
	Root := filescan.New(fpath)
	var result []byte
	var err error
	var flist []filescan.FileMap

	wg.Add(1)
	go func() {
		defer wg.Done()
		task.Filefound(&Root, MaxLine, &wg)
	}()
	wg.Wait()
	flist = make([]filescan.FileMap, 100000)

	L := filescan.Field(&Root, flist, 100000, ChoseType.String(), fpath)
	var outputfile struct {
		Rootpath string             `json:"rootpath"` //根目录路径
		Filelist []filescan.FileMap `json:"filelist"` //文件列表

	}
	outputfile.Filelist = flist[:L]
	outputfile.Rootpath = Root.RootDir
	result, err = json.Marshal(outputfile)

	if err != nil {
		fmt.Printf("Map转化为byte数组失败,异常:%s\n", err)
		panic(err)
	}
	err = os.WriteFile(outputname, result, 0644)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%s\nDone!!", fpath)

}
